package com.gastronomy.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gastronomy.dao.InformationDao;
import com.gastronomy.dao.UserDao;
import com.gastronomy.entity.InformationEntity;
import com.gastronomy.entity.User;
import com.gastronomy.service.InformationService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @description 资讯service 实现类
 * 
 * @compny 深圳风轮科技有限公司
 * 
 * @author Guo PengFei
 * 
 * @date 2018-01-29
 * 
 * @version V1.0
 * 
 */
@Service("informationService")
public class InformationServiceImpl implements InformationService {

	private Logger logger = Logger.getLogger(InformationServiceImpl.class);

	@Autowired
	private InformationDao informationDao;

	@Autowired
	private UserDao userDao;
	/**
	 * 获取所有的热门资讯
	 * 
	 * @param currentPage
	 *            当前页码
	 * @param pageSize
	 *            每页的条数
	 * @return
	 */
	@Override
	public List<InformationEntity> listInformations(Integer currentPage,
			Integer pageSize) {
		List<InformationEntity> resultList = new ArrayList<InformationEntity>();

		Page<?> page = PageHelper.startPage(currentPage, pageSize);

		try {
			resultList = informationDao.listInformations();
		} catch (Exception e) {
			logger.error("=====errror info :" + e.getMessage());
		}
		return resultList;
	}

	/**
	 * 根据id查找一篇资讯
	 * 
	 * @param informationId
	 *            资讯id
	 * @return
	 */
	@Override
	public InformationEntity getInformationById(String informationId) {

		InformationEntity informationEntity = informationDao
				.getInformationById(informationId);

		return informationEntity;

	}

	@Override
	public void updateInformationFocuse(InformationEntity informationEntity) {

		if(informationEntity == null )
			return ;
		informationEntity.setFocuse(informationEntity.getFocuse()+1);
		
		informationDao.updateInformationFocuse(informationEntity);
	}

	@Override
	public Map<String, String> saveInformation(User user,
			InformationEntity informationEntity) {
		Map<String, String> resultMap = new HashMap<String, String>();
		if( user.getRole().getId() !=2){
			resultMap.put("addResult", "FAIL");
			resultMap.put("errorMsg","您没有权限进行此操作");
			return resultMap;
		}
		
		User currentUser = userDao.findUserByNameAndPwd(user.getUserName(), user.getPassword());
		if(currentUser == null){
			resultMap.put("addResult", "FAIL");
			resultMap.put("errorMsg","您没有权限进行此操作");
			return resultMap;
		}
		
		informationEntity.setCreateTime(new Timestamp(new Date().getTime()));
		informationEntity.setUser(currentUser);
		informationEntity.setFocuse(0);
		try {
			informationDao.saveInformation(informationEntity);
		} catch (Exception e) {
			resultMap.put("addResult", "FAIL");
			resultMap.put("errorMsg","发布资讯失败，请稍后再试");
			return resultMap;
		}
		resultMap.put("addResult", "SUCCESS");
		return resultMap;
	}

}
